/**
 * @file 路由守卫页面
 * @author wangshaozhi(wangshaozhi@baidu.com)
 */
import React from 'react';
import {Redirect, Route} from 'react-router-dom';
import type {RouteProps} from 'react-router-dom';

export const RouteGuard: React.FC<RouteProps> = ({
    component,
    ...rest
}) => {
    if (!component) {
        return null;
    }
    const Component = component;
    const hasToken: boolean = !!localStorage.getItem('token');

    return (
        <Route
            {...rest}
            render={props => (
                hasToken
                ? <Component {...props} />
                : <Redirect to="/login" />
            )}
        />
    );
}